﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using l1cs.server.model.skill;
using l1cs.server.model.Instance;
using l1cs.server.serverpackets;

namespace l1cs.server.model
{
    public class L1Cube
    {
        private static Logger _log = Logger.getLogger( typeof( L1Cube ).FullName );

        private Timer _future = null;

        private int _timeCounter = 0;

        private L1Character _effect;

        private L1Character _cha;

        private int _skillId;

        public L1Cube( L1Character effect, L1Character cha, int skillId )
        {
            _effect = effect;
            _cha = cha;
            _skillId = skillId;
        }

        public void L1CubeThreadSub( object state )
        {
            if ( _cha.isDead() )
            {
                stop();
                return;
            }
            if ( !_cha.hasSkillEffect( _skillId ) )
            {
                stop();
                return;
            }
            _timeCounter++;
            giveEffect();
        }

        public void begin()
        {
            // 効果時間が8秒のため、4秒毎のスキルの場合処理時間を考慮すると実際には1回しか効果が現れない
            // よって開始時間を0.9秒後に設定しておく
            //_future = GeneralThreadPool.getInstance().scheduleAtFixedRate( this,  );
            _future = new Timer( new TimerCallback( L1CubeThreadSub ), null, 900, 1000 );
        }

        public void stop()
        {
            if ( _future != null )
            {
                _future.Dispose();
            }
        }

        public void giveEffect()
        {
            if ( _skillId == L1SkillId.STATUS_CUBE_IGNITION_TO_ENEMY )
            {
                if ( _timeCounter % 4 != 0 )
                {
                    return;
                }
                if ( _cha.hasSkillEffect( L1SkillId.STATUS_FREEZE ) )
                {
                    return;
                }
                if ( _cha.hasSkillEffect( L1SkillId.ABSOLUTE_BARRIER ) )
                {
                    return;
                }
                if ( _cha.hasSkillEffect( L1SkillId.ICE_LANCE ) )
                {
                    return;
                }
                if ( _cha.hasSkillEffect( L1SkillId.FREEZING_BLIZZARD ) )
                {
                    return;
                }
                if ( _cha.hasSkillEffect( L1SkillId.FREEZING_BREATH ) )
                {
                    return;
                }
                if ( _cha.hasSkillEffect( L1SkillId.EARTH_BIND ) )
                {
                    return;
                }

                if ( _cha is L1PcInstance )
                {
                    L1PcInstance pc = (L1PcInstance)_cha;
                    pc.sendPackets( new S_DoActionGFX( pc.getId(),
                            ActionCodes.ACTION_Damage ) );
                    pc.broadcastPacket( new S_DoActionGFX( pc.getId(),
                            ActionCodes.ACTION_Damage ) );
                    pc.receiveDamage( _effect, 10, false );
                }
                else if ( _cha is L1MonsterInstance )
                {
                    L1MonsterInstance mob = (L1MonsterInstance)_cha;
                    mob.broadcastPacket( new S_DoActionGFX( mob.getId(),
                            ActionCodes.ACTION_Damage ) );
                    mob.receiveDamage( _effect, 10 );
                }
            }
            else if ( _skillId == L1SkillId.STATUS_CUBE_QUAKE_TO_ENEMY )
            {
                if ( _timeCounter % 4 != 0 )
                {
                    return;
                }
                if ( _cha.hasSkillEffect( L1SkillId.STATUS_FREEZE ) )
                {
                    return;
                }
                if ( _cha.hasSkillEffect( L1SkillId.ABSOLUTE_BARRIER ) )
                {
                    return;
                }
                if ( _cha.hasSkillEffect( L1SkillId.ICE_LANCE ) )
                {
                    return;
                }
                if ( _cha.hasSkillEffect( L1SkillId.FREEZING_BLIZZARD ) )
                {
                    return;
                }
                if ( _cha.hasSkillEffect( L1SkillId.FREEZING_BREATH ) )
                {
                    return;
                }
                if ( _cha.hasSkillEffect( L1SkillId.EARTH_BIND ) )
                {
                    return;
                }

                if ( _cha is L1PcInstance )
                {
                    L1PcInstance pc = (L1PcInstance)_cha;
                    pc.setSkillEffect( L1SkillId.STATUS_FREEZE, 1000 );
                    pc.sendPackets( new S_Paralysis( S_Paralysis.TYPE_BIND, true ) );
                }
                else if ( _cha is L1MonsterInstance )
                {
                    L1MonsterInstance mob = (L1MonsterInstance)_cha;
                    mob.setSkillEffect( L1SkillId.STATUS_FREEZE, 1000 );
                    mob.setParalyzed( true );
                }
            }
            else if ( _skillId == L1SkillId.STATUS_CUBE_SHOCK_TO_ENEMY )
            {
                // if (_timeCounter % 5 != 0) {
                // return;
                // }
                // _cha.addMr(-10);
                // if (_cha instanceof L1PcInstance) {
                // L1PcInstance pc = (L1PcInstance) _cha;
                // pc.sendPackets(new S_SPMR(pc));
                // }
                _cha.setSkillEffect( L1SkillId.STATUS_MR_REDUCTION_BY_CUBE_SHOCK, 4000 );
            }
            else if ( _skillId == L1SkillId.STATUS_CUBE_BALANCE )
            {
                if ( _timeCounter % 4 == 0 )
                {
                    int newMp = _cha.getCurrentMp() + 5;
                    if ( newMp < 0 )
                    {
                        newMp = 0;
                    }
                    _cha.setCurrentMp( newMp );
                }
                if ( _timeCounter % 5 == 0 )
                {
                    if ( _cha is L1PcInstance )
                    {
                        L1PcInstance pc = (L1PcInstance)_cha;
                        pc.receiveDamage( _effect, 25, false );
                    }
                    else if ( _cha is L1MonsterInstance )
                    {
                        L1MonsterInstance mob = (L1MonsterInstance)_cha;
                        mob.receiveDamage( _effect, 25 );
                    }
                }
            }
        }
    }
}
